package com.example.hkt.service;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
/**
 * @Author: zhanglm
 * @CreateDate: 2025-03-24 9:23
 * @FullNameClass: com.example.hkt.service.BaiduOcrService
 * @Description:调用百度OCR测试使用
 */
@Service
public class BaiduOcrService {

    private final RestTemplate restTemplate;
    private final String apiKey;
    private final String secretKey;
    private final String ocrUrl = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic";

    @Autowired
    public BaiduOcrService(RestTemplate restTemplate, @Value("${baidu.api.key}") String apiKey, @Value("${baidu.secret.key}") String secretKey) {
        this.restTemplate = restTemplate;
        this.apiKey = apiKey;
        this.secretKey = secretKey;
    }

    public String generalBasic(String image) {
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(ocrUrl)
                .queryParam("access_token", getAccessToken())
                .queryParam("image", image); // 这里image应该是Base64编码的字符串或URL
        String url = builder.toUriString();
        return restTemplate.postForObject(url, null, String.class);
    }

    private String getAccessToken() {
        // 这里省略了获取access_token的方法实现，通常需要调用https://aip.baidubce.com/oauth/2.0/token接口获取access_token
        // 需要处理API Key和Secret Key的安全存储和定期刷新access_token逻辑。
        return "your_access_token"; // 示例值，实际使用时需动态获取或缓存access_token。
    }
}
